<template>
	<view class="wrap">
		<view class="modItem">
			<view class="modTit spaceBet">问题标题<view class="clear" @tap="clearTit">清空</view></view>
			<textarea class="clsNameInp" v-model="queTit" placeholder="请输入问题标题(40字内)" maxlength="40" auto-height />
		</view>
		<view class="modItem">
			<view class="modTit spaceBet">问题描述<view class="clear" @tap="clearCon">清空</view></view>
			<textarea class="queConInp" v-model="queCon" placeholder="请输入问题描述(1000字内)" maxlength="1000" />
		</view>
		<view class="queRule" @tap="viewQueRule">提问规则</view>
		<button class="comBtn" :disabled="isDisabledFlag" @tap="pubQue">发布问题</button> 
	</view>
</template>

<script>
	import { addMind } from '@/api/ansQue/index'
	export default {
		data(){
			return{
				queTit:'',
				queCon:'',
				nmTag:1,//是否匿名 1否 2是
				isDisabledFlag:false,
				hasPubFlag:false
			}
		},
		onShow(){
			var pubQueField = uni.getStorageSync('pubQueField');
			if(pubQueField){
				this.queTit = pubQueField.queTit;
				this.queCon = pubQueField.queCon;
			}
		},
		onUnload(){
			if(this.queTit != '' && !this.hasPubFlag){
				var field = { queTit:this.queTit,queCon:this.queCon }
				uni.setStorageSync('pubQueField',field);
			}
		},
		methods:{
			viewQueRule(){
				uni.navigateTo({
					url:'../../protocol/protocol?xyId=6'
				})
			},
			pubQue(){
				if(this.queTit == ''){
					this.util.showToast('请输入问题标题');
				}else if(this.queCon == ''){
					this.util.showToast('请输入问题描述');
				}else{
					uni.showLoading({title:'问题发布中...', mask: true });
					this.isDisabledFlag = true;
					var field = { askTitle:this.queTit,askContent:this.util.replaceToBr(this.queCon),nmTag:this.nmTag };
					addMind(field).then(res=>{
						this.util.showToastSuc('发布成功');
						this.hasPubFlag = true;
						uni.removeStorageSync('pubQueField');
						let pages = getCurrentPages();
						let prevPage = pages[pages.length - 2];
						prevPage.$vm.isRefreshFlag = true;
						setTimeout(()=>{
							uni.navigateBack({
								delta:1
							})
						},1500);
					}).catch(err=>{
						this.isDisabledFlag = false;
						console.log(err)
					});
				}
			},
			clearTit(){
				this.queTit = '';
				if(this.queCon == ''){
					uni.removeStorageSync('pubQueField');
				}
			},
			clearCon(){
				this.queCon = '';
				if(this.queTit == ''){
					uni.removeStorageSync('pubQueField');
				}
			}
		}
	}
</script>

<style lang='scss' scoped>
	.queRule{
		font-size: 28rpx;
		color: $pss-color-green;
		text-align: right;
	}
	.comBtn{
		margin: 100rpx auto 0;
		border-radius: 50rpx;
		background: $pss-color-blueDeep;
	}
	.modItem{
		margin-bottom: 20rpx;
	}
	.clsNameInp,
	.queConInp{
		width: 96%;
		background: $pss-bg-gray;
		font-size: $font-size30;
		color: $pss-text-color3;
		border-radius: 10rpx;
		padding: 24rpx 2%;
	}
	.queConInp{
		height: 500rpx;
	}
	.modTit{
		height: 90rpx;
		line-height: 90rpx;
		font-size: $font-size32;
		color: $pss-text-color3;
		font-weight: bold;
		.clear{
			font-size:$font-size28;
			color: #20d3a8;
			height: 100%;
			padding-left: 50rpx;
			font-weight: normal;
		}
	}
	.wrap{
		padding: 10rpx 30rpx 0;
	}
</style>